<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

if ($_POST["withdraw_class_form_submit"] && $_SESSION["user"]["member_id"]>0){
	$reorder_participants = false;
	$class_capacity = NULL;
	
	if(isset($_POST["withdraw_class_form_participant"])){
		$participants_pkey=mysql_real_escape_string(trim($_POST["withdraw_class_form_participant"]),$class_manager_db);
		
		$result = mysql_query("SELECT * FROM participants WHERE pkey='$participants_pkey'",$class_manager_db) or die(mysql_error($class_manager_db));
		if(mysql_num_rows($result) > 0){
			$line=mysql_fetch_array($result,MYSQL_ASSOC);
			if($line["forum_member_id"] == $_SESSION["user"]["member_id"]){
				if($line["status"] != "kicked"){
					$class_pkey=$line["class"];
					$capacity_result = mysql_query("SELECT capacity FROM classes WHERE pkey='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
					if(mysql_num_rows($capacity_result) > 0){
						$capacity_line=mysql_fetch_array($capacity_result,MYSQL_ASSOC);
						$class_capacity = $capacity_line["capacity"];
					}
					else{
						e("Critical Error: No associated class found for withdrawing participant.");
					}
					
					
					
					if($line["status"] == "registered" && $class_capacity != NULL){
							$reorder_participants = true;
					}
				}
				else{
					e("You have been kicked from this class, please contact an admin about changing your status.");
				}
			}
			else {
				e("You are not authorized to change this record.");
			}
		}
		else {
			e("pkey not found during withdrawl request.");
		}
	}
	else{
		e("No pkey sent with withdrawl request.");
	}
	
	if(empty($errors)){
		mysql_query("LOCK TABLE participants WRITE", $class_manager_db) or die(mysql_error($class_manager_db));
		
		mysql_query("UPDATE participants SET status='withdrawn' WHERE pkey='$participants_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		
		if($reorder_participants){
			$result = mysql_query("SELECT COUNT(pkey) as num_registered FROM participants WHERE status='registered' AND alumnus='0' AND class='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
			$line=mysql_fetch_array($result,MYSQL_ASSOC);
			$num_to_admit = $class_capacity - $line["num_registered"];
			if($num_to_admit > 0){
				$result = mysql_query("SELECT pkey FROM participants WHERE status='wait_list' AND class=$class_pkey ORDER BY timestamp ASC", $class_manager_db) or die(mysql_error($class_manager_db));
				while(($line=mysql_fetch_array($result,MYSQL_ASSOC)) && $num_to_admit>0){
					mysql_query("UPDATE participants SET status='registered' WHERE pkey='".$line["pkey"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
					$num_to_admit--;
				}
			}
		}
		mysql_query("UNLOCK TABLES", $class_manager_db) or die(mysql_error($class_manager_db));
		s("You have successfully withdrawn.");
	}
}	

if ($_POST["reinstate_class_form_submit"] && $_SESSION["user"]["member_id"]>0){
	if(isset($_POST["reinstate_class_form_participant"])){
		$participants_pkey=mysql_real_escape_string(trim($_POST["reinstate_class_form_participant"]),$class_manager_db);
		$result = mysql_query("SELECT * FROM participants WHERE pkey='$participants_pkey'",$class_manager_db) or die(mysql_error($class_manager_db));
		if(mysql_num_rows($result) > 0){
			$line=mysql_fetch_array($result,MYSQL_ASSOC);
			if($line["forum_member_id"] == $_SESSION["user"]["member_id"]){
				if($line["status"] != "kicked"){
					$class_pkey=$line["class"];
					$capacity_result = mysql_query("SELECT capacity FROM classes WHERE pkey='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
					if(mysql_num_rows($capacity_result) > 0){
						$capacity_line=mysql_fetch_array($capacity_result,MYSQL_ASSOC);
						$class_capacity = $capacity_line["capacity"];
					}
					else{
						e("Critical Error: No associated class found for reinstating participant.");
					}
				}
				else{
					e("You have been kicked from this class, please contact an admin about changing your status.");
				}
			}
			else {
				e("You are not authorized to change this record.");
			}
		}
		else {
			e("pkey not found during reinstate request.");
		}
	}
	else{
		e("No pkey sent with reinstate request.");
	}
	
	
	
	if(empty($errors)){
		mysql_query("LOCK TABLE participants WRITE", $class_manager_db) or die(mysql_error($class_manager_db));
		$result = mysql_query("SELECT COUNT(pkey) as num_registered FROM participants WHERE status='registered' AND alumnus='0' AND class='$class_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		$line=mysql_fetch_array($result,MYSQL_ASSOC);
		if($class_capacity === NULL || $line["num_registered"] < $class_capacity){
			mysql_query("UPDATE participants SET status='registered' WHERE pkey='$participants_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		}
		else {
			mysql_query("UPDATE participants SET status='wait_listed' WHERE pkey='$participants_pkey'", $class_manager_db) or die(mysql_error($class_manager_db));
		}
		mysql_query("UNLOCK TABLES", $class_manager_db) or die(mysql_error($class_manager_db));
		s("You have successfully withdrawn.");
	}
	
	
}	
?>
